/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package BeanSession;

import Clases.Encrypt;
import Dao.DaoEmployee;
import HibernateUtil.HibernateUtil;
import Pojo.Employees;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author knyazev_o
 */
@Named(value = "mbSLogin")
@SessionScoped
public class MbSLogin implements Serializable{
    /**
     * Creates a new instance of MbSLogin
     */
    private String login;
    private String password;    
    private Session session;
    private Transaction transaction;
    
    public MbSLogin() 
    {
        HttpSession miSession=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        miSession.setMaxInactiveInterval(5000);
    }
    
    public String login()
    {
        this.session=null;
        this.transaction=null;
        
        try
        {
            DaoEmployee daoEmployee=new DaoEmployee();
            
            this.session=HibernateUtil.getSessionFactory().openSession();
            this.transaction=this.session.beginTransaction();            
            Employees employees=daoEmployee.getByLogin(this.session, this.login);            
            if(employees!=null)
            {
                if(employees.getPassword().equals(Encrypt.sha512(this.password)))
                {
                    if(employees.isActual()){
                        HttpSession httpSession=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                        httpSession.setAttribute("login", this.login);                    
                        return "/employee/hello";
                    }else{
                        this.transaction.commit();           
                        this.login=null;
                        this.password=null;            
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка доступа:", "Данный пользователь не имеет прав"));
                        return "/index";
                    }                    
                }
            }            
            this.transaction.commit();           
            this.login=null;
            this.password=null;            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ошибка доступа:", "Неправильный пользователь или пароль!"));
            return "/index";            
        }
        catch(Exception ex)
        {
            this.login=null;
            this.password=null;
            if(this.transaction!=null)
            {
                this.transaction.rollback();
            }            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Фатальная ошибка:", "Пожалуйста, обратитесь к администратору "+ex.getMessage()));
            return null;
        }
        finally
        {
            if(this.session!=null)
            {
                this.session.close();
            }
        }
    }
    
    public String endSession()
    {
        this.login=null;
        this.password=null;        
        HttpSession httpSession=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        httpSession.invalidate();        
        return "/index";
    }
    
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;       
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


}
